package com.neusoft.elm_servlet.controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.neusoft.elm_servlet.po.DeliveryAddress;
import com.neusoft.elm_servlet.service.DeliveryAddressService;
import com.neusoft.elm_servlet.service.impl.DeliveryAddressServiceImpl;

public class DeliveryAddressController {
	
	DeliveryAddressService deliveryAddressService = new DeliveryAddressServiceImpl();

	public List<DeliveryAddress> listDeliveryAddressByUserId(HttpServletRequest request,HttpServletResponse response) throws SQLException{
		String userId = request.getParameter("userId");
		List<DeliveryAddress> list = deliveryAddressService.listDeliveryAddressByUserId(userId);
		return list;
	}
	
	public int saveDeliveryAddress(HttpServletRequest request,HttpServletResponse response){
		//contactName、contactSex、contactTel、address、userId
		String contactName = request.getParameter("contactName");
		String contactSex = request.getParameter("contactSex");
		String contactTel = request.getParameter("contactTel");
		String address = request.getParameter("address");
		String userId = request.getParameter("userId");
		
		DeliveryAddress da = new DeliveryAddress();
		da.setAddress(address);
		da.setContactName(contactName);
		da.setContactSex(Integer.valueOf(contactSex));
		da.setContactTel(contactTel);
		da.setUserId(userId);
		
		int count = 0;
		try {
			count = deliveryAddressService.saveDeliveryAddress(da);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
	
	public DeliveryAddress getDeliveryAddressById(HttpServletRequest request,HttpServletResponse response){
		String daId = request.getParameter("daId");
		DeliveryAddress da = null;
		try {
			da = deliveryAddressService.getDeliveryAddressById(daId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return da;
	}
	
	public int updateDeliveryAddress(HttpServletRequest request,HttpServletResponse response){
		//daId、contactName、contactSex、contactTel、address、userId
		String daId = request.getParameter("daId");
		String contactName = request.getParameter("contactName");
		String contactSex = request.getParameter("contactSex");
		String contactTel = request.getParameter("contactTel");
		String address = request.getParameter("address");
		String userId = request.getParameter("userId");
		
		DeliveryAddress da = new DeliveryAddress();
		da.setDaId(Integer.valueOf(daId));
		da.setAddress(address);
		da.setContactName(contactName);
		da.setContactSex(Integer.valueOf(contactSex));
		da.setContactTel(contactTel);
		da.setUserId(userId);
		
		int count = 0;
		try {
			count = deliveryAddressService.updateDeliveryAddress(da);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count;
	}
	
	public int removeDeliveryAddress(HttpServletRequest request,HttpServletResponse response){
		String daId = request.getParameter("daId");
		int count = 0;
		try {
			count = deliveryAddressService.removeDeliveryAddress(daId);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return count;
	}
}
